/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TestNeuro_View_Grid.java
 *
 * Created on 13/08/2012, 08:54:26 PM
 */
package v;

import c.MedicoController;
import core.constructor.constructor;
import java.util.Map;
import javax.swing.JPanel;

/**
 *
 * @author Usuario
 */
public class TestNeuro_View_Grid extends javax.swing.JPanel {

    private constructor c;
    private MedicoController controller = new MedicoController();
    
    private String idPaciente = null;
    private String idMedico = null;
    private String idAcudiente = null;
    
    private String idCitas;
    
    private static final Integer CITA_ACTIVA = 1;
    
    /** Creates new form TestNeuro_View_Grid */
    public TestNeuro_View_Grid(Object... e) {
        initComponents();
        config(e);
        textoEspecialidad.setEditable(false);
        textoEdad.setEditable(false);
        textoPrimera.setEditable(false);
        textoTel.setEditable(false);
        nombresMedico.setEditable(false);
        nombresPaciente.setEditable(false);
        identificacionMedico.setEditable(false);
        identificacionPaciente.setEditable(false);
    }
    
    private void config(Object... e) {
        if (e.length > 0) {
            c = (constructor) e[0];
        }

        //Asignar SQL para Tabla
        c.setLabelModulo("Test Neuropsicopedagogico");
        c.setLabelAccion("Revisar Test");
        bSelecPaciente.setEnabled(false);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mensaje = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jXTitledSeparator2 = new org.jdesktop.swingx.JXTitledSeparator();
        jLabel3 = new javax.swing.JLabel();
        nombresMedico = new javax.swing.JTextField();
        bSelecMedic = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        identificacionMedico = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jXTitledSeparator3 = new org.jdesktop.swingx.JXTitledSeparator();
        jLabel5 = new javax.swing.JLabel();
        nombresPaciente = new javax.swing.JTextField();
        bSelecPaciente = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        identificacionPaciente = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        textoEdad = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        textoPrimera = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        textoEspecialidad = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        textoTel = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        resultado = new javax.swing.JPanel();

        mensaje.setFont(new java.awt.Font("Tahoma", 1, 14));

        jXTitledSeparator2.setFont(new java.awt.Font("Tahoma", 1, 15));
        jXTitledSeparator2.setHorizontalTextPosition(javax.swing.SwingConstants.LEADING);
        jXTitledSeparator2.setTitle("Medico");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel3.setText("Nombres");

        nombresMedico.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        bSelecMedic.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        bSelecMedic.setIcon(new javax.swing.ImageIcon(getClass().getResource("/core/img/busqueda32.png"))); // NOI18N
        bSelecMedic.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bSelecMedicActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel4.setText("Identificación");
        jLabel4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel4MouseClicked(evt);
            }
        });

        identificacionMedico.setFont(new java.awt.Font("Tahoma", 0, 14));

        jXTitledSeparator3.setFont(new java.awt.Font("Tahoma", 1, 15));
        jXTitledSeparator3.setHorizontalTextPosition(javax.swing.SwingConstants.LEADING);
        jXTitledSeparator3.setTitle("Paciente");

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel5.setText("Nombres");

        nombresPaciente.setFont(new java.awt.Font("Tahoma", 0, 14));

        bSelecPaciente.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        bSelecPaciente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/core/img/busqueda32.png"))); // NOI18N
        bSelecPaciente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bSelecPacienteActionPerformed(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel6.setText("Identificación");

        identificacionPaciente.setFont(new java.awt.Font("Tahoma", 0, 14));

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel9.setText("Edad");
        jLabel9.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel9MouseClicked(evt);
            }
        });

        textoEdad.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        jLabel10.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel10.setText("Primera vez");
        jLabel10.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel10MouseClicked(evt);
            }
        });

        textoPrimera.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jXTitledSeparator3, javax.swing.GroupLayout.DEFAULT_SIZE, 1006, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(18, 18, 18)
                        .addComponent(nombresPaciente, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE)
                        .addGap(6, 6, 6)
                        .addComponent(bSelecPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(10, 10, 10)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(identificacionPaciente, javax.swing.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(textoEdad, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(textoPrimera, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE)
                        .addGap(23, 23, 23))))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jXTitledSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel6))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(7, 7, 7)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(identificacionPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(textoEdad, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(textoPrimera, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(4, 4, 4)
                                .addComponent(jLabel5))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(1, 1, 1)
                                .addComponent(nombresPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(bSelecPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(4, 4, 4)
                                .addComponent(jLabel10)))))
                .addContainerGap())
        );

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel7.setText("Especialidad");
        jLabel7.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel7MouseClicked(evt);
            }
        });

        textoEspecialidad.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel8.setText("Telefono");
        jLabel8.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel8MouseClicked(evt);
            }
        });

        textoTel.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jXTitledSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 1008, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(nombresMedico, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
                        .addGap(10, 10, 10)
                        .addComponent(bSelecMedic, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(10, 10, 10)
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(identificacionMedico, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(10, 10, 10)
                        .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(textoEspecialidad, javax.swing.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(textoTel, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                        .addGap(26, 26, 26))
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(35, 35, 35))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addComponent(jXTitledSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(4, 4, 4)
                                .addComponent(jLabel3))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(1, 1, 1)
                                .addComponent(nombresMedico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(bSelecMedic, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(4, 4, 4)
                                .addComponent(jLabel4))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(4, 4, 4)
                                .addComponent(jLabel7))
                            .addComponent(textoEspecialidad, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(4, 4, 4)
                                .addComponent(jLabel8))
                            .addComponent(textoTel, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(7, 7, 7)
                        .addComponent(identificacionMedico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(4, 4, 4))
        );

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        resultado.setLayout(new java.awt.GridBagLayout());
        jScrollPane1.setViewportView(resultado);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1020, Short.MAX_VALUE)
                .addGap(23, 23, 23))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(2, 2, 2)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 423, Short.MAX_VALUE)
                .addGap(27, 27, 27))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void bSelecMedicActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bSelecMedicActionPerformed
    
        SeleccionadorIU select = new SeleccionadorIU(
                "SELECT id_medico, identificacion, nombres, apellidos, telefono1, "
                + "especialidad.descripcion FROM "
                + "medico natural join especialidad", "CODIGO,CEDULA,NOMBRES,APELLIDOS,TELEFONO,ESPECIALIDAD", c, this.getClass().getName(), "cargar");
        select.setVisible(true);
//        select.setBounds(0, 0, 453, 189);
        //        cargar(select.getDatosSelect());
        
        Map<String, String> datosSelect = select.getDatosSelect();
        
        if(datosSelect.isEmpty()) return;
        
        bSelecPaciente.setEnabled(true);
        
        nombresMedico.setText(datosSelect.get("NOMBRES") + " " + datosSelect.get("APELLIDOS"));
        identificacionMedico.setText(datosSelect.get("CEDULA"));
        idMedico = datosSelect.get("CODIGO");
        textoEspecialidad.setText(datosSelect.get("ESPECIALIDAD"));
        textoTel.setText(datosSelect.get("TELEFONO"));
    }
    
    //No usado
    private void cargar(Map<String, String> dato) {
        System.out.println("id = " + dato.get("CODIGO"));
        System.out.println("cc = " + dato.get("CEDULA"));
        System.out.println("nombre = " + dato.get("NOMBRES"));
        
        if(dato.isEmpty()) return;
        
        String idPaciente = dato.get("CODIGO");
        String paciente = dato.get("NOMBRES") + " " + dato.get("APELLIDOS");
        nombresMedico.setText(paciente);
        identificacionMedico.setText(dato.get("CEDULA"));
        
        String select = String.format("SELECT id, id_acudiente, id_medico FROM "
                + "test_neuropsicopedagogico WHERE id_paciente = '%s' limit(1)", idPaciente);
        
        Object[][] buscar = controller.buscar(select);
        System.out.println("Size: " + buscar.length);
        if(buscar.length == 0){
            resultado.removeAll();
            mensaje.setText("No hay ningun Test asociado a " + paciente);
            resultado.add(mensaje);
            resultado.validate();
        }else{
            String id = buscar[0][0].toString();
            String idAcudiente = buscar[0][1].toString();
            String idMedico = buscar[0][2].toString();
            
            //Se le podria pasar solo el id
            TestNeuro_View_Add testNeuro_View_Add =
                    new TestNeuro_View_Add(c,"SELECT", id, 
                            idPaciente, idAcudiente, idMedico);
            resultado.removeAll();
            resultado.add(testNeuro_View_Add);
            resultado.validate();
            resultado.updateUI();
        }
    }
        //c.ventanaEmergente("SELECT id_acudiente,id_identificacion,nombres FROM acudiente", "CODIGO,CEDULA,NOMBRES", c , this.getClass().getName() , "cargar");          SeleccionadorIU select = new SeleccionadorIU("SELECT id_acudiente,identificacion,nombres FROM acudiente", "CODIGO,CEDULA,NOMBRES", c, this.getClass().getName(), "cargar");         select.setVisible(true);         select.setBounds(0, 0, 453, 189);         cargar(select.getDatosSelect());      }//GEN-LAST:event_bSelecMedicActionPerformed

private void bSelecPacienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bSelecPacienteActionPerformed

//    "select distinct ct.id, paciente.id_paciente, "
//            + "paciente.identificacion, paciente.nombres, paciente.apellidos, "
//            + "paciente.id_acudiente, extract(YEAR from "
//            + "(current_date - paciente.fecha_nacimiento)), "
//            + "(select hora from cita natural join citas_paciente where "
//            + "citas_paciente.id = ct.id order by hora limit 1) as hora from "
//            + "paciente, cita, planilla_horario, citas_paciente as ct "
//            + "where planilla_horario.fecha_agendada = current_date and "
//            + "hora > current_time and ct.id_cita = cita.id_cita and "
//            + "planilla_horario.id_medico = %s and  "
//            + "planilla_horario.id_planilla_horario = cita.id_planilla_horario "
//            + " and cita.estado = %d" //Cual es estado activo cita?
//            + "and  cita.id_paciente is not null and  "
//            + "cita.id_paciente = paciente.id_paciente order by hora;", 
    
//    + "paciente.id_acudiente, extract(YEAR from "
//            + "(current_date - paciente.fecha_nacimiento)), "
    
    String SQL = String.format("select distinct ct.id, paciente.id_paciente, "
            + "paciente.identificacion, paciente.nombres, paciente.apellidos, "
            + "paciente.id_acudiente, extract (year from "
            + "age(paciente.fecha_nacimiento)), "
            + "(select to_char(hora, 'HH12:MI AM') from cita natural join citas_paciente where "
            + "citas_paciente.id = ct.id order by hora limit 1) as hora from "
            + "paciente, cita, planilla_horario, citas_paciente as ct "
            + "where planilla_horario.fecha_agendada = current_date and "
            + " ct.id_cita = cita.id_cita and "
            + "planilla_horario.id_medico = %s and  "
            + "planilla_horario.id_planilla_horario = cita.id_planilla_horario "
            + " and cita.estado = %d" //Cual es estado activo cita?
            + "and  cita.id_paciente is not null and  "
            + "cita.id_paciente = paciente.id_paciente order by hora;", 
            idMedico, CITA_ACTIVA);
    
    SeleccionadorIU select = new SeleccionadorIU(SQL,
            "IDCITA,CODIGO,CEDULA,NOMBRES,APELLIDOS,IDACUDIENTE,EDAD,HORA", c,
            this.getClass().getName(), "cargar");
        
    select.setVisible(true);
//    select.setBounds(0, 0, 453, 189);
    //        cargar(select.getDatosSelect());

    Map<String, String> datosSelect = select.getDatosSelect();

    if (datosSelect.isEmpty()) {
        return;
    }
    
    nombresPaciente.setText(datosSelect.get("NOMBRES") + " " + datosSelect.get("APELLIDOS"));
    identificacionPaciente.setText(datosSelect.get("CEDULA"));
    textoEdad.setText(datosSelect.get("EDAD"));

    idPaciente = datosSelect.get("CODIGO");
    idAcudiente = datosSelect.get("IDACUDIENTE");
    idCitas = datosSelect.get("IDCITA");

        
    verificacion();
    
}//GEN-LAST:event_bSelecPacienteActionPerformed

private void jLabel4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel4MouseClicked
//Para pruebas
    //    Diagnostico_View_Add diagnostico =
//                new Diagnostico_View_Add(c, 1,
//                2);
//
//        cargarVista(diagnostico);
}//GEN-LAST:event_jLabel4MouseClicked

private void jLabel7MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel7MouseClicked
// TODO add your handling code here:
}//GEN-LAST:event_jLabel7MouseClicked

private void jLabel8MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel8MouseClicked
// TODO add your handling code here:
}//GEN-LAST:event_jLabel8MouseClicked

private void jLabel9MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel9MouseClicked
// TODO add your handling code here:
}//GEN-LAST:event_jLabel9MouseClicked

private void jLabel10MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel10MouseClicked
// TODO add your handling code here:
}//GEN-LAST:event_jLabel10MouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bSelecMedic;
    private javax.swing.JButton bSelecPaciente;
    private javax.swing.JTextField identificacionMedico;
    private javax.swing.JTextField identificacionPaciente;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private org.jdesktop.swingx.JXTitledSeparator jXTitledSeparator2;
    private org.jdesktop.swingx.JXTitledSeparator jXTitledSeparator3;
    private javax.swing.JLabel mensaje;
    private javax.swing.JTextField nombresMedico;
    private javax.swing.JTextField nombresPaciente;
    private javax.swing.JPanel resultado;
    private javax.swing.JTextField textoEdad;
    private javax.swing.JTextField textoEspecialidad;
    private javax.swing.JTextField textoPrimera;
    private javax.swing.JTextField textoTel;
    // End of variables declaration//GEN-END:variables

    public void cargarVista(JPanel panel) {
        resultado.removeAll();
        resultado.add(panel);
        resultado.validate();
        resultado.updateUI();        
    }

    //TODO si la cita es asistida faltando 5 minutos de la fecha limite y 
    //dura 15 min el sistema puede cambiarla automaticamente a no asistida antes
    //de que el usuario guarde el estado de la cita
    private void verificacion() {
        
        if (null == idAcudiente || null == idPaciente || null == idMedico) {
            return;
        }
        
        String SQL_test = String.format("SELECT id, id_paciente FROM test_neuropsicopedagogico WHERE "
                + "id_paciente = '%s'",idPaciente);
        Object[][] buscarTest = controller.buscar(SQL_test);
        
        String SQL_Trat = String.format("SELECT id_tratamiento, id_paciente FROM tratamiento "
                + "where id_paciente = '%s'", idPaciente);
        Object[][] buscarDiag = controller.buscar(SQL_Trat);
        
        System.out.println("Size: " + buscarDiag.length);
        textoPrimera.setText("SI");
        if(buscarTest.length == 0){
            TestNeuro_View_Add testNeuro_View_Add =
                    new TestNeuro_View_Add(c, "INSERT", null,
                    idPaciente, idAcudiente, idMedico, this, idCitas);

            cargarVista(testNeuro_View_Add);
            
//            resultado.removeAll();
//            mensaje.setText("TODO Test asignado, cargar ventana de evolución");
//            resultado.add(mensaje);
//            resultado.revalidate();
//            resultado.repaint();
        }else if(buscarDiag.length == 0){
            Diagnostico_View_Add diagnostico =
                    new Diagnostico_View_Add(c, Integer.valueOf(idPaciente),
                    Integer.valueOf(idMedico), idCitas);

            cargarVista(diagnostico);
        }else{
            textoPrimera.setText("NO");
            Integer idpaciente = Integer.valueOf(idPaciente);
            Evaluacion_View_Add evaluacion = new Evaluacion_View_Add(
                    idpaciente, nombresPaciente.getText(), 
                    identificacionPaciente.getText(), c, idCitas);
            cargarVista(evaluacion);
        }
    }
}
